package com.prs.crm.action.fs;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.fs.BankAccount;
import com.prs.crm.domain.fs.Detail;
import com.prs.crm.service.fs.BankAccountService;
import com.prs.crm.service.fs.DetailService;

public class DetailAction extends BaseAction{
	private Detail detail;
	private DetailService detailService;
	private PaginationSupport pagination;
	
	private BankAccount bankAccount;
	private BankAccountService bankAccountService;
	private Collection<BankAccount> bankAccounts;
	
	private Double totalIncome;
	private Double totalPay;
	private Double totalBalance;
	
	@Action("listDetail")
	public String listDetail() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Detail.class);
		criteria.addOrder(Order.desc("item"));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDetailService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("viewDetail")
	public String viewDetail() {
		return SUCCESS;
	}
	
	@Action("listBankAccounts")
	public String listBankAccounts() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(BankAccount.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getBankAccountService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("listAccountDetail")
	public String listAccountDetail() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Detail.class);
		criteria.add(Restrictions.eq("bankAccount", bankAccount));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDetailService().listByPage(criteria, pagination));
		
		totalIncome = this.getDetailService().getSUMIncomeByAccount(bankAccount);
		totalPay = this.getDetailService().getSUMPayByAccount(bankAccount);
		totalBalance = this.getDetailService().getBalanceByAccount(bankAccount);
		return SUCCESS;
	}
	
	public Detail getDetail() {
		return detail;
	}
	public void setDetail(Detail detail) {
		this.detail = detail;
	}
	public DetailService getDetailService() {
		return detailService;
	}
	public void setDetailService(DetailService detailService) {
		this.detailService = detailService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public BankAccount getBankAccount() {
		return bankAccount;
	}

	public void setBankAccount(BankAccount bankAccount) {
		this.bankAccount = bankAccount;
	}

	public BankAccountService getBankAccountService() {
		return bankAccountService;
	}

	public void setBankAccountService(BankAccountService bankAccountService) {
		this.bankAccountService = bankAccountService;
	}

	public Collection<BankAccount> getBankAccounts() {
		return bankAccounts;
	}

	public void setBankAccounts(Collection<BankAccount> bankAccounts) {
		this.bankAccounts = bankAccounts;
	}

	public Double getTotalIncome() {
		return totalIncome;
	}

	public void setTotalIncome(Double totalIncome) {
		this.totalIncome = totalIncome;
	}

	public Double getTotalPay() {
		return totalPay;
	}

	public void setTotalPay(Double totalPay) {
		this.totalPay = totalPay;
	}

	public Double getTotalBalance() {
		return totalBalance;
	}

	public void setTotalBalance(Double totalBalance) {
		this.totalBalance = totalBalance;
	}
	
}
